from django.shortcuts import render, redirect, HttpResponse

from app01 import models
from app01 import page


def home(request):
    """登录页"""
    if not request.session.get('user'):
        return redirect('login')
    query_position = models.Position.objects.all()
    ###############    分页组件固定用法     #############
    current_page = request.GET.get('page')
    page_object = page.Pagination(current_page=current_page,
                                  all_count=query_position.count(),
                                  base_url=request.path_info,
                                  query_params=request.GET,
                                  per_page=20,
                                  )
    queryset = query_position[page_object.start:page_object.end]
    page_html = page_object.page_html()

    return render(request, 'home.html', context={'query_position': queryset, 'page_html': page_html})


def delete_operation(request, pk):
    """删除"""
    if not request.session.get('user'):
        return redirect('login')
    print(pk)
    query = models.Position.objects.filter(is_delete=False, is_show=True, pk=pk)
    exist = query.exists()
    if exist:
        query.update(is_delete=True, is_show=False)
    else:
        models.Position.objects.filter(is_delete=True, is_show=False, pk=pk).update(is_delete=False, is_show=True)
    return redirect('home')
